package com.example.stella.utils;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * 会话管理器 - 使用SharedPreferences管理用户登录状态
 */
public class SessionManager {
    private static final String PREF_NAME = "StellaSession";
    private static final String KEY_IS_LOGGED_IN = "isLoggedIn";
    private static final String KEY_USER_ID = "userId";
    private static final String KEY_USERNAME = "username";

    private SharedPreferences pref;
    private SharedPreferences.Editor editor;
    private Context context;

    public SessionManager(Context context) {
        this.context = context;
        pref = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        editor = pref.edit();
    }

    /**
     * 创建用户登录会话
     */
    public void createLoginSession(int userId, String username) {
        editor.putBoolean(KEY_IS_LOGGED_IN, true);
        editor.putInt(KEY_USER_ID, userId);
        editor.putString(KEY_USERNAME, username);
        editor.commit();
    }

    /**
     * 检查用户是否已登录
     */
    public boolean isLoggedIn() {
        return pref.getBoolean(KEY_IS_LOGGED_IN, false);
    }

    /**
     * 获取当前登录用户ID
     */
    public int getUserId() {
        return pref.getInt(KEY_USER_ID, -1);
    }

    /**
     * 获取当前登录用户名
     */
    public String getUsername() {
        return pref.getString(KEY_USERNAME, null);
    }

    /**
     * 用户退出登录，清除所有会话数据
     */
    public void logoutUser() {
        editor.clear();
        editor.commit();
    }
} 